﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;

namespace QuanLySanXuat.Controllers
{
    public class CategoryController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly ICategoryRepository categoryRepository;
        public CategoryController(ICommandBus commandBus, ICategoryRepository categoryRepository)
        {
            this.commandBus = commandBus;
            this.categoryRepository = categoryRepository;
        }
        //
        // GET: /Category/
        [HttpGet]
        public ActionResult Index()
        {
            ModelState.Clear();
            var prodcutType = categoryRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(int ID, string Name, string Description, string Remark)
        {
            var form = new CategoryFormModels()
            {
                ID = ID,
                Name = Name,
                Description = Description,
                Remark = Remark,
            };
            var command = Mapper.Map<CategoryFormModels, CreateOrUpdateCategoryCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            var result = commandBus.Submit(command);
            var model = categoryRepository.GetAll();
            return PartialView("_CategoryList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public JsonResult Get(int ID)
        {
            Category product = categoryRepository.GetById(ID);
            var obj = Mapper.Map<Category, CategoryFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteCategoryCommand> list = new List<DeleteCategoryCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteCategoryCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = categoryRepository.GetAll();
            return PartialView("_CategoryList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Category> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = categoryRepository.GetAll();
            }
            else
            {
                model = categoryRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Description.Contains(Name)) || (x.Remark.Contains(Name)));

            }
            return PartialView("_CategoryList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = categoryRepository.GetAll();
            return PartialView("_CategoryList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
